/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tcnplanner;

import java.util.ArrayList;
import java.util.HashMap;
import javax.persistence.*;

/**
 *
 * @author Koen
 */
public class AccountService {

    EntityManager em = null;
    private HashMap<String, Account> accounts;

    public AccountService(EntityManager em) {
        this.em = em;
        accounts = new HashMap<String, Account>();
        //Person test = new Person(","Henk","Datank","0123456789", "Hank@datank.nl");
        Account acc1 = new Account("Engineer", "test", Controller.ActorType.ENGINEER, "Henk", "Henkstraat 2", "0123456789", "Hank@datank.nl");
        this.create(acc1);
        Account acc2 = new Account("Klant", "test", Controller.ActorType.INTERNCUSTOMER, "Henk", "Henkstraat 2", "0123456789", "Hank@datank.nl");
        this.create(acc2);
        Account acc3 = new Account("Operator", "test", Controller.ActorType.OPERATOR, "Henk", "Henkstraat 2", "0123456789", "Hank@datank.nl");
        this.create(acc3);
        Account acc4 = new Account("Planner", "test", Controller.ActorType.PLANNER, "Henk", "Henkstraat 2", "0123456789", "Hank@datank.nl");
        this.create(acc4);
        Account acc5 = new Account("SFC", "test", Controller.ActorType.EXTERNCUSTOMER, "Henk", "Henkstraat 2", "0123456789", "Hank@datank.nl");
        this.create(acc5);
        Account acc6 = new Account("LongN", "test", Controller.ActorType.EXTERNCUSTOMER, "Long Nyogen", "Rachelsmolen 1", "0123456789", "Long@fontys.nl");
        this.create(acc6);
        Account acc7 = new Account("AlbertL", "test", Controller.ActorType.EXTERNCUSTOMER, "Albert Lak", "Rachelsmolen 1", "0123456789", "Albert@fontys.nl");
        this.create(acc7);
        Account acc8 = new Account("TomasS", "test", Controller.ActorType.EXTERNCUSTOMER, "Tomas van Summeren", "Rachelsmolen 1", "0123456789", "Tomas@fontys.nl");
        this.create(acc8);
        Account acc9 = new Account("TomasS", "test", Controller.ActorType.EXTERNCUSTOMER, "Tomas van Summeren", "Rachelsmolen 1", "0123456789", "Tomas@fontys.nl");
        this.create(acc9);
        Account acc10 = new Account("BobE", "test", Controller.ActorType.EXTERNCUSTOMER, "Bob van Eijk", "Rachelsmolen 1", "0123456789", "Bob@fontys.nl");
        this.create(acc10);
        Account acc11 = new Account("KoenS", "test", Controller.ActorType.PLANNER, "Koen Staal", "Rachelsmolen 1", "0123456789", "Koen@fontys.nl");
        this.create(acc11);
    }

    public boolean create(Account a) {
        if (this.findbyUsername(a.getUsername()) == null) {
            em.getTransaction().begin();
            em.persist(a);
            accounts.put(a.getUsername(), a);
            em.getTransaction().commit();
            return true;
        }
        return false;
    }

    public boolean edit(Account a) {
        if (this.findbyUsername(a.getUsername()) != null) {
            em.getTransaction().begin();
            accounts.put(a.getUsername(), a);
            em.flush();             // Niet zeker meer?
            em.getTransaction().commit();
            return true;
        }
        return false;
    }

    public Account find(String username) {
        return this.findbyUsername(username);
    }

    public ArrayList<Account> findAll() {
        //Iterator acc = accounts.values().iterator();
//        ArrayList<Account> accountarray = new ArrayList<Account>();
//        while (acc.hasNext()) {
//            accountarray.add((Account) acc.next());
//        }
        Query q = em.createNamedQuery("account.findAll", Account.class);
        ArrayList<Account> accountarray = (ArrayList) q.getResultList();
        return accountarray;
    }

    public void remove(Account a) {
        em.remove(a.getUsername());
    }

    public void removeAll() {
        em.clear();
    }

    private Account findbyUsername(String username) {
        Query q = em.createNamedQuery("Account.findbyUsername", Account.class);
        q.setParameter("username", username);
        if (q.getResultList().isEmpty()) {
            return null;
        } else {
            return (Account) q.getSingleResult();
        }
    }
}
